<?php

declare(strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use app\common\tool\Result;
use app\common\model\User;
//该文件为自动生成，请按需修改
class DealOrder extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\DealOrder';

    protected array $hasOne = ['user', 'buser'];

    protected array $append = ['statusText','typeText'];
    public function index(): string
    {
        $param = $this->request->param();
        if(isset($param['id'])){
            $extra_map[] = ['deal_id', '=', $param['id']];
        }
        $extra_map[] = ['status', '>', 0];
        $search = [
            ['uid', '卖家会员编号/昵称', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
            ['buy_uid', '买家会员编号/昵称', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
            ['status', '状态', 'select', $this->model::getStatusList('', 2)],
            ['type', '交易类型', 'select', $this->model::getDealType('', 2)],
        ];
        return Builder::Table()
            ->autoStructure($this->model, $this->pk, 'dealDemo,260')
            ->addExtraMap($extra_map)
            ->addSearch($search)
            ->addRightButtons([
                'repeal' => ['title' => '撤销', 'event' => '_submit', 'url' => url('dealOrder/orderEdit')->build() . '?type=clear'],
                'receipt' => ['title' => '确认收款', 'event' => '_submit',  'url'=>url('dealOrder/orderEdit')->build() . '?type=receipt'],
                'delete'
            ])->addTopButtons([
                'export'])
            ->addExtraHtml(['deal'])
            ->fetch();
    }
    /**
     * 修改状态
     */
    public function orderEdit()
    {
        try {
            $param = $this->request->param();
            $info = $this->model::find($param['id']);
            if (empty($info)) {
                return Result::fail('数据不存在');
            }
            switch ($param['type']) {
                case 'clear': //撤销订单
                    $user = User::find($info['buy_uid']);
                    break;
                case "receipt": //确认收款
                    $user = User::find($info['uid']);
                    break;
                default:
                return Result::fail(['加载失败']);
            }
            return Result::fastReturn($this->model::edit($param,$user));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
